package com.easyweb.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easyweb.cache.StartUpListener;
import com.easyweb.common.Constants;
import com.easyweb.common.PrimaryKeyGenerator;
import com.easyweb.common.PrintUtils;
import com.easyweb.model.FriendLink;
import com.easyweb.model.ResultMsg;
import com.easyweb.model.Users;
import com.easyweb.service.FriendLinkService;

@Controller
@RequestMapping("/friendlink")
public class FriendLinkController {
	
	@Autowired
	private FriendLinkService friendLinkService;
	
	@RequestMapping(value="/merge",method=RequestMethod.POST)
	public void addNavBar(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		if(StringUtils.isEmpty(title)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("title.notnull")));
			return;
		}
		String url = request.getParameter("url");
		if(StringUtils.isEmpty(url)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("url.notnull")));
			return;
		}
		FriendLink friendLink = new FriendLink();
		if(StringUtils.isEmpty(id)){
			//新增
			friendLink.setId(PrimaryKeyGenerator.generatePrimaryKey());
			friendLink.setSortNum(1);
		}else{
			friendLink.setId(id);
		}
		friendLink.setTitle(title);
		friendLink.setUrl(url);
		int num = 0;
		if(StringUtils.isEmpty(id)){
			num = friendLinkService.insertFriendLink(friendLink);
		}else{
			num = friendLinkService.updateFriendLink(friendLink);
		}
		if(num > 0){
			StartUpListener.refreshFriendLinkCache(friendLinkService, friendLink.getId());
			PrintUtils.printMsg(response, new ResultMsg());
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,null));
		}
	}
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public void getNavBars(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String pageSize = request.getParameter("pageSize");
		String pageStart = request.getParameter("pageStart");
		Map<String, Object> map = new HashMap<String, Object>();
		if(!StringUtils.isEmpty(pageSize)){
			map.put("pageSize", Integer.parseInt(pageSize));
		}
		if(!StringUtils.isEmpty(pageStart)){
			map.put("start",  Integer.parseInt(pageSize)*(Integer.parseInt(pageStart)-1));
		}
		PrintUtils.printMsg(response, friendLinkService.getFriendLinks(map));
	}
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public void getNavBarById(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		PrintUtils.printMsg(response, friendLinkService.getFriendLinkById(id));
	}
	
	@RequestMapping(value="/del",method=RequestMethod.GET)
	public void delNavBarById(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		map.put("id", id);
		int num = 0;
		num = friendLinkService.deleteFriendLink(map);
		if(num > 0){
			StartUpListener.refreshFriendLinkCache(friendLinkService, id);
			PrintUtils.printMsg(response, new ResultMsg());
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,null));
		}
	}
}
